//
//  ARradarViewController.m
//  POCRA
//
//  Created by Marcel Arbó Lack on 26/11/10.
//  Copyright 2010 Mobivery. All rights reserved.
//


#import "ARradarViewController.h"
#import "ARViewController.h"

@implementation ARradarPoint

@synthesize pointImage;
@synthesize pointazimuth;
@synthesize pointRadio;

- (void)dealloc {
	
	[pointImage release];
    [super dealloc];
	
}

@end

@implementation ARradarViewController

- (void)dealloc {
	
	[pointsViews release];
	[arrayPoints release];
	[radarsImage release];
	
    [super dealloc];
}

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
		arrayPoints = [[NSMutableArray alloc] init];
    }
    return self;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)setRadarPoints:(NSMutableArray*)arCoordinates MaxScaleDistance:(double)scaleDist {
	maxScaleDistance = scaleDist;
	
	//pointsViews.frame = CGRectMake(0.0, 0.0, 89.0, 89.0);
	
	for (int i=0; i<[arCoordinates count]; i++) {
		ARradarPoint *pointV;
		ARCoordinate *arcoords;
		double pradio;
		
		arcoords = (ARCoordinate *)[arCoordinates objectAtIndex:i];
		pointV = [[ARradarPoint alloc] init];
		UIImageView * image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"punto-amarillo.png"]];
		[pointV setPointImage:image];
		
		if (arcoords.azimuth < 0.0) {
			
			[pointV setPointazimuth:(2*M_PI) - arcoords.azimuth];
			
		} else {
			
			[pointV setPointazimuth:M_PI - arcoords.azimuth];
			
		}
		
		pradio = (ABS(arcoords.radialDistance) * kMaxRadarRadio)/maxScaleDistance;
		[pointV setPointRadio:pradio];
		
		double y = pradio * sin(pointV.pointazimuth);
		double x = pradio * cos(pointV.pointazimuth);
		
		//NSLog(@"x:%f y:%f",x,y);
		
		[pointsViews addSubview:pointV.pointImage];
		pointV.pointImage.frame = CGRectMake(39.0 + x, 39.0 - y, pointV.pointImage.frame.size.width, pointV.pointImage.frame.size.height);
		
		//CGRect frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
		
		[arrayPoints addObject:pointV];
		
		[image release];
		[pointV release];
	}
}

- (void)setRadarPointsArray:(NSMutableArray*)arCoordinates MaxScaleDistance:(double)scaleDist {
	maxScaleDistance = scaleDist;
	
	for (int i=0; i<[arCoordinates count]; i++) {
		ARradarPoint *pointV;
		ARCoordinate *arcoords;
		double pradio;
		
		arcoords = (ARCoordinate *)[arCoordinates objectAtIndex:i];
		pointV = [[ARradarPoint alloc] init];
		[pointV setPointImage:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"punto-amarillo.png"]] autorelease]];
		if (arcoords.azimuth < 0.0) {
			[pointV setPointazimuth:(2*M_PI) - arcoords.azimuth];
		} else {
			[pointV setPointazimuth:M_PI - arcoords.azimuth];
		}
		
		pradio = (ABS(arcoords.radialDistance) * kMaxRadarRadio)/maxScaleDistance;
		[pointV setPointRadio:pradio];
		
		double y = pradio * sin(pointV.pointazimuth);
		double x = pradio * cos(pointV.pointazimuth);
		
		pointV.pointImage.frame = CGRectMake(39.0 + x, 39.0 - y, pointV.pointImage.frame.size.width, pointV.pointImage.frame.size.height);
		
		[arrayPoints addObject:pointV];
		[pointV release];
	}
}

- (void)addPointsToRadarView {
	ARradarPoint *point;
	
	for (int i=0; i<[arrayPoints count]; i++) {
		point = (ARradarPoint*)[arrayPoints objectAtIndex:i];
		[pointsViews addSubview:point.pointImage];
	}
}

- (void)moveRadarPoints:(double)centerAsimuth {
	
	double angle = (centerAsimuth);
	
	pointsViews.transform = CGAffineTransformMakeRotation(-angle + (M_PI/2));
	//NSLog(@"x%f y:%f w:%f h:%f",pointsViews.frame.origin.x,pointsViews.frame.origin.y,pointsViews.frame.size.width,pointsViews.frame.size.height);
}

- (void)resetRadar {
	ARradarPoint *point;
	
	for (int i=0; i<[arrayPoints count]; i++) {
		point = (ARradarPoint*)[arrayPoints objectAtIndex:i];
		[point.pointImage removeFromSuperview];
	}
	
	[arrayPoints removeAllObjects];
}

- (void)movePointToSuperview {
	[self.view bringSubviewToFront:pointsViews];
}

- (void)changeRadarImage:(BOOL)IsLandscapeMode {
	if (IsLandscapeMode == YES) {
		[radarsImage setImage:[UIImage imageNamed:@"RA_radar53.png"]];
	} else {
		[radarsImage setImage:[UIImage imageNamed:@"RA_radar.png"]];
	}

}


@end
